<?php defined('_JEXEC') or die; 
// shortcuts
$buyer= $this->order->buyer;
    
// this function is similar to the one defined in turtle template for user view
function P($property,$value=null,$delimiter=null)
{
    if (!empty($value)){;
        $result = $property;
        switch ($delimiter) {
            case 'S': // string
                $result .=  ' "' . $value . '"';
                break;
            case 'T': // text
                $result .=  ' """' . $value . '"""';
                break;
            case 'U': // uri
                $result .=  ' <' . $value . '>';
                break;
            case 'C': // curie
                $result .=  ' :' . $value ;
                break;           
            default: 
                $result .=  ' ' . $value;
                break;
        }
        $result .=  " ; \n";
    } else {
        $result =  "#  no $property\n";
    }
    
    return $result;
}
?>
<?=$this->getTurtlePrefixes()?> 
@prefix : <#> .

<?=$this->getDefaultMetadata()?>
<>  foaf:primaryTopic :this ;
    void:inDataset <<?=UriHelper::mkResourceURI('orders',null,'dataset')?>> ;
.
:this a b2bo:PurchaseOrder ;
    dcterms:identifier "<?=$buyer->order_number?>" ;
    b2bo:hasSeller <<?=UriHelper::mkResourceURI('user',$this->order->vendor->virtuemart_user_id,'this')?>>  ;
    b2bo:hasBuyer :buyer_<?=$buyer->virtuemart_order_userinfo_id?> ;
    <?=P('rdfs:comment', $buyer->customer_note, 'T')?>
    b2bo:hasAmount [ a   b2bo:DocumentAmount ;
        b2bo:hasCurrency "<?=$this->order->vendor->currencyCode?>" ;
        b2bo:toPay <?=$buyer->order_total?> ;
    ] ;
.

:buyer_<?=$buyer->virtuemart_order_userinfo_id?> a gr:BusinessEntity ;
<?php if (!empty($buyer->virtuemart_user_id)): ?>
    owl:sameAs <<?=UriHelper::mkResourceURI('user',$buyer->virtuemart_user_id,'this')?>> ; 
<?php endif?>
    <?=P('gr:taxID', $buyer->vatid,'S')?>
    <?=P('gr:vatID', $buyer->taxid,'S')?>
<?php if (empty($buyer->company)): ?>
    a vcard:Individual ;
    vcard:fn """<?=$buyer->fn?>""" ;
    vcard:hasFN [
        a vcard:Name;
        <?=P('vcard:honorific-prefix',  $buyer->title,'T')?>
        <?=P('vcard:given-name',        $buyer->first_name,'T')?>
        <?=P('vcard:additional-name',   $buyer->middle_name,'T')?>
        <?=P('vcard:family-name',       $buyer->last_name,'T')?>
    ] ;
<?php else :?>
    a vcard:Organization;
    vcard:fn """<?=$buyer->company?>""" ;
    <?php if ($buyer->fn): ?>vcard:related [
        a vcard:Kind ;
        vcard:fn """<?=$buyer->fn?>""" ;
    ] ;<?php endif?>   
<?php endif?>
<?php 
if (empty($buyer->deliveryInfo)){
    printVcard($buyer, ':InvoicePoint, :DeliveryPoint');    
} else {
    printVcard($buyer, ':InvoicePoint');    
    printVcard($buyer->deliveryInfo, ':DeliveryPoint');       
}?>  
.

# Order items
<?php foreach( $this->order->items as $key=>$item):?>
:this b2bo:hasItem :item_<?=$item->virtuemart_order_item_id ?> .
:item_<?=$item->virtuemart_order_item_id ?> a b2bo:Item ;
    b2bo:hasNumber <?=$key?> ;
    b2bo:hasDataLocker <<?=UriHelper::mkResourceURI('product',$item->virtuemart_product_id,'item')?>> ;
    b2bo:hasSKU "<?=$item->order_item_sku?>" ;
    dcterms:description """<?=trim($item->order_item_name . ' ' . $item->product_attribute)?>""" ;
    b2bo:quantity <?=$item->product_quantity?> ;
    dcterms:subject o:productCategory_<?=$item->virtuemart_category_id?> ;
    b2bo:hasAmount [ a   b2bo:ItemAmount ;
        b2bo:hasCurrency "<?=$this->order->vendor->currencyCode?>" ;
        b2bo:includesNetUnitPrice <?=$item->product_item_price?> ;
        b2bo:includesGrossValue <?=$item->product_subtotal_with_tax?> ;
        b2bo:includesVATAmount <?=$item->product_tax?> ;
        b2bo:includesNetValue <?=$item->product_subtotal_without_tax?> ;
        b2bo:includesVATRate <?=$item->taxRate?> ;
    ] ;
.
<?php endforeach ?>

# Local taxonomies to categorize vcard properies
:InvoicePoint a skos:Concept ;
    skos:prefLabel "Invoice contact for order <?=$buyer->order_number?>" ;
    skos:broader ebactor::InvoicePoint ;
.
:DeliveryPoint a skos:Concept ;
    skos:prefLabel "Delivery contact for order <?=$buyer->order_number?>" ;
    skos:broader ebactor::DeliveryPoint ;
.

<?php 
function printVcard ($vcard,$addressType) {?>
<?php if (!empty($vcard->email)): ?>
    vcard:hasEmail [
        a vcard:Email ;
        dcterms:subject <?=$addressType?> ;
        vcard:hasValue "<?=$vcard->email?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->phone_1)): ?>
    vcard:hasTelephone [
        a vcard:Voice ;
        dcterms:subject <?=$addressType?> ;
        vcard:hasValue "<?=$vcard->phone_1?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->phone_2)): ?>
    vcard:hasTelephone [
        a vcard:Voice ;
        dcterms:subject <?=$addressType?> ;
        vcard:hasValue "<?=$vcard->phone_2?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->fax)): ?>
    vcard:hasTelephone [
        a vcard:Fax ;
        dcterms:subject <?=$addressType?> ;
        vcard:hasValue "<?=$vcard->fax?>" 
    ] ;
<?php endif?>
<?php if (!empty($vcard->address_1) || !empty($vcard->address_2) || !empty($vcard->city)|| !empty($vcard->zip)):?>
    vcard:hasAddress [
        a vcard:Address ;
        dcterms:subject <?=$addressType?> ;
        <?=P('vcard:locality',$vcard->city,'S')?>
        <?=P('vcard:postal-code',$vcard->zip,'S')?>
        <?=P('vcard:country-name',$vcard->country,'S')?>
        <?=P('vcard:region',$vcard->state,'S')?>
        <?=P('vcard:street-address',trim($vcard->address_1. ' '. $vcard->address_2),'T')?>
    ] ;
<?php endif?> 
<?php 
} // end of function printVcard
if(JDEBUG){
    echo "#------------------ START DEBUGGING INFO -------------------------\n#";
    echo preg_replace("/\n/", "\n#", ApiHelper::debugInfo($this));
    echo "\n#------------------ END DEBUGGING INFO -------------------------\n";
}
?>
